package com.controller;

import com.pojo.Bill;
import com.pojo.Goods;
import com.service.CheckerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("checker")
public class CheckerController {
    @Autowired
    private CheckerService checkerService;
    @RequestMapping("index")
    public ModelAndView Index(){
        List<Goods> goods = checkerService.getGoods();
        ModelAndView mv= new ModelAndView("checker/index");
        mv.addObject("goods",goods);
        return mv;
    }
    @RequestMapping(value = "sell",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String Sell(  Integer goodsid,Integer goodscount, Integer isPayment, Double zj, Integer userid){
        Bill bill = new Bill();
        bill.setGoodsId(goodsid);
        bill.setProductCount(goodscount);
        bill.setIsPayment(isPayment);
        bill.setTotalPrice(zj);
        bill.setUserId(userid);
        int i = checkerService.insertBill(bill);
        int i1=0;
        if (isPayment==2){
            i1=checkerService.updateGoodsCount(bill);
        }
        if (i>0&&i1>0){
            return "购买成功";
        }
        if (i>0&&i1==0){
            return "加入购物车成功";
        }
        return "操作失败";
    }
    @RequestMapping("shoppingcartLoad")
    public ModelAndView shoppingCart(Integer userid){
        List<Bill> bills = checkerService.shoppingCart(userid);
        ModelAndView mv= new ModelAndView("checker/shoppingcart");
        mv.addObject("bills",bills);
        return mv;
    }

    @RequestMapping(value = "deleteShopping",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String deleteShopping(Integer id){
        int i = checkerService.deleteShopping(id);
        if (i>0){
            return "删除成功";
        }
        return "删除失败";
    }
    @RequestMapping(value = "goShopping",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String goShopping(Integer id){

          int i  = checkerService.updateBill(id);

        if (i>0){
            return "购买成功";
        }
        return "购买失败";
    }
}
